import {useTableActionRedux} from "@/components/ant-ext-table/redux/HAdminContext";
import {useActionForm} from "@/common/hooks";
import HModal from "@/components/ant-ext/HModal";
import {Form} from "antd";
import HFormItem from "@/components/ant-ext/HFormItem";
import SysOrg from "@/api/sys-org";
import {AdminStat} from "@/const/const";
import SysAdmin, {SysAdminEntity} from "@/api/sys-admin";
import {Key, useEffect, useState} from "react";
import SysRole from "@/api/sys-role";

export default function AdminEdit() {
    const [loading, setLoading] = useState(false)
    const [expandedAuthOrgCodeKeys, setExpandedAuthOrgCodeKeys] = useState<Key[]>([])
    const [expandedOrgCodeKeys, setExpandedOrgCodeKeys] = useState<Key[]>([])
    const {action, reset} = useTableActionRedux<SysAdminEntity>()
    const {form, key, title} = useActionForm(action, [SysAdmin.EDIT], SysAdmin.MAPPING)
    const close = () => {
        reset()
        form.resetFields()
    }
    useEffect(() => {
        if (action.match(SysAdmin.EDIT)) {
            SysAdmin.selectById({
                params: {code: action.getRow()?.adminCode}, setLoading, success: (data) => {
                    form.setFieldsValue(data)
                    setExpandedAuthOrgCodeKeys(data.authOrgCode)
                    setExpandedOrgCodeKeys([data.orgCode])
                }
            })
        }
    }, [action, form])


    return <>
        <HModal onCancel={close} type={"setUp"} width={500} onOk={form.submit}
                confirmLoading={loading}
                visible={title !== undefined}
                title={title}
        >

            <Form name={"editForm"} form={form} labelCol={{span: 5}}
                  onFinish={() => {
                      SysAdmin.doEdit({
                          body: form.getFieldsValue(), setLoading, success: () => {
                              close()
                          }
                      })
                  }}>
                <HFormItem label={"编号"} name={"adminCode"} hidden={key === SysAdmin.ADD}
                           disabled={key !== SysAdmin.ADD}/>
                <HFormItem label={"名称"} name={"adminName"} required/>
                <HFormItem label={"登录名"} name={"loginName"} required/>
                <HFormItem label={"登陆密码"} name={"loginPwd"} required/>
                <HFormItem label={"角色编码"} name={"roleCode"} type={"select"} dataSource={SysRole.selectList}
                           required/>
                <HFormItem label={"隶属组织"} name={"orgCode"} type={"treeSelect"}
                           treeExpandedKeys={expandedOrgCodeKeys} onTreeExpand={setExpandedOrgCodeKeys}
                           dataSource={SysOrg.selectTree}
                           required/>
                <HFormItem label={"数据权限"} name={"authOrgCode"} type={"treeSelect"} multiple maxTagCount={1}
                           treeExpandedKeys={expandedAuthOrgCodeKeys} onTreeExpand={setExpandedAuthOrgCodeKeys}
                           dataSource={SysOrg.selectTree}/>
                <HFormItem label={"状态"} type={"radio"} dataSource={AdminStat.select} name={"stat"}
                           required/>

            </Form>
        </HModal>
    </>
}